﻿

using System.Collections.Generic;
using System;
using hcache;
using hentity;

namespace htaskCommon {
    public abstract class AllocateJobTask : DeviceCode
    {
  
        public abstract List<BaseTask> analyseTaskModel(AbsStatus gcs);
        public abstract Type getSynchro();
        public override  void excute()
        {
            var type = getSynchro();
            AbsStatus gcs = StatusCache.getStatus(type);
            if( gcs == null ) {
                return;
            }
            perCheck();
            if( !gcs.isIdle() ) {
                consoleOnly(this.className+"不是空闲状态");
                return;
            }
            if( !chechModeDB() ) {
                return;
            }
            List<BaseTask> robotTask = analyseTaskModel(gcs);
            if (robotTask != null && robotTask.Count > 0 )
            {
                DoOutAndShiftTask(robotTask);
            }
            else
            {
                robotTask = preExecution();
                if (robotTask != null&& robotTask .Count>0)
                {
                    DoOutAndShiftTask(robotTask);
                }
                else
                {
                    consoleOnly("isIdle");
                }
            }
        }
      
         public virtual bool chechModeDB() {
            return true;
        }
         public virtual void perCheck()
        {
           
        }
         public virtual List<BaseTask> preExecution()
        {
            return new List<BaseTask>();
        }
       
      public   List<T> getTask<T>() where  T : BaseTask {
             List<T> res = new List<T>();
            List<CTaskDto<BaseTask>> list = TaskCacheDB.getList(typeof(T));
            foreach( CTaskDto<BaseTask> bt in list ) {
                if( bt.t.status == 9 ) {
                    continue;
                }

                if( !bt.canWork() ) {
                    continue;
                }
               
                if( DeviceId()==bt.t.driveId  ) {
                    res.Add(( T ) bt.t);
                }
            }

            this.consoleOnly("共{0}个任务，可执行{1}个{2}",list.Count,res.Count,typeof(T).Name);
            return res;
        }
        public bool working(int status) {
            if( status > ( int ) CoreTaskParamStatus.待执行 && status < ( int ) CoreTaskParamStatus.执行结束 ) {
                return true;
             } else {
                return false;
            }
        }
    }
}
